<template>
  <div>
    <zh-card title="">
      <zh-form :model="pageData.queryParams" ref="queryFormRef">
        <zh-form-input prop="mapId" label="表格映射主键" type="text" :span="8" dataType="common"
                       v-model="pageData.queryParams.mapId"></zh-form-input>
        <zh-form-input prop="tableName" label="表格名称" type="text" :span="8" dataType="common"
                       v-model="pageData.queryParams.tableName"></zh-form-input>
        <zh-form-input prop="dataSourcename" label="数据源名称" type="text" :span="8" dataType="common"
                       v-model="pageData.queryParams.dataSourcename"></zh-form-input>
        <zh-form-input prop="datasourceId" label="所属数据源" type="text" :span="8" dataType="common"
                       v-model="pageData.queryParams.datasourceId"></zh-form-input>
        <zh-form-input prop="srcTableId" label="源表格表id" type="text" :span="8" dataType="common"
                       v-model="pageData.queryParams.srcTableId"></zh-form-input>
        <zh-form-input prop="srcTableName" label="源表格名称" type="text" :span="8" dataType="common"
                       v-model="pageData.queryParams.srcTableName"></zh-form-input>
        <zh-form-input prop="srcColumnName" label="字段名称" type="text" :span="8" dataType="common"
                       v-model="pageData.queryParams.srcColumnName"></zh-form-input>
        <zh-form-input prop="mapColumnName" label="映射字段名称" type="text" :span="8" dataType="common"
                       v-model="pageData.queryParams.mapColumnName"></zh-form-input>
        <zh-form-input prop="srcColumnType" label="字段类型" type="text" :span="8"
                        v-model="pageData.queryParams.srcColumnType"></zh-form-input>
        <zh-form-input prop="mapTableName" label="映射逻辑表格名称" type="text" :span="8" dataType="common"
                       v-model="pageData.queryParams.mapTableName"></zh-form-input>
        <zh-form-input prop="mapColumnType" label="映射字段类型" type="text" :span="8" dataType="common"
                        v-model="pageData.queryParams.mapColumnType"></zh-form-input>
        <zh-form-input prop="srcIdColumnName" label="源表格主键字段名称" type="text" :span="8" dataType="common"
                       v-model="pageData.queryParams.srcIdColumnName"></zh-form-input>
        <zh-form-query @queryClick="queryLogicMap" :formRef="queryFormRef" :show-advanced="false">
          <el-button @click="showAddDialog">添加</el-button>
        </zh-form-query>
      </zh-form>
    </zh-card>
    <zh-table title="逻辑表映射列表" ref="logicMapTable"
              size="mini"
              :funcCode="'logicMap0001'"
              :columns="pageData.tableColumns"
              highlight-current-row>
      <template #op="scope">
        <el-tooltip content="编辑" placement="top">
          <el-button :icon="Edit" link @click="showEditDialog(scope)"></el-button>
        </el-tooltip>
        <el-tooltip content="删除" placement="top">
          <el-button :icon="Delete" type="danger" link @click="delLogicMap(scope.row)"></el-button>
        </el-tooltip>
<!--        <el-tooltip content="详情" placement="top">
          <el-button :icon="Document" link @click="showDetail(scope)"></el-button>
        </el-tooltip>-->
        <el-tooltip content="执行" placement="top">
          <el-button :icon="VideoPlay" link @click="updateLogicTableData(scope.row)"></el-button>
        </el-tooltip>
      </template>
    </zh-table>
    <el-dialog title="添加逻辑表映射" v-model="pageData.addLogicMapDialogVisible">
      <zh-form :rules="pageData.formRules">
        <zh-form-input prop="mapId" label="表格映射主键" type="text" :span="8" dataType="common"
                       v-model="pageData.formData.mapId"></zh-form-input>
        <zh-form-input prop="tableName" label="表格名称" type="text" :span="8" dataType="common"
                       v-model="pageData.formData.tableName"></zh-form-input>
        <zh-form-input prop="dataSourcename" label="数据源名称" type="text" :span="8" dataType="common"
                       v-model="pageData.formData.dataSourcename"></zh-form-input>
        <zh-form-input prop="datasourceId" label="所属数据源" type="text" :span="8" dataType="common"
                       v-model="pageData.formData.datasourceId"></zh-form-input>
        <zh-form-input prop="srcTableId" label="源表格表id" type="text" :span="8" dataType="common"
                       v-model="pageData.formData.srcTableId"></zh-form-input>
        <zh-form-input prop="srcTableName" label="源表格名称" type="text" :span="8" dataType="common"
                       v-model="pageData.formData.srcTableName"></zh-form-input>
        <zh-form-input prop="srcColumnName" label="字段名称" type="text" :span="8" dataType="common"
                       v-model="pageData.formData.srcColumnName"></zh-form-input>
        <zh-form-input prop="mapColumnName" label="映射字段名称" type="text" :span="8" dataType="common"
                       v-model="pageData.formData.mapColumnName"></zh-form-input>
        <zh-form-input prop="srcColumnType" label="字段类型" type="text" :span="8" dataType="common"
                        v-model="pageData.formData.srcColumnType"></zh-form-input>
        <zh-form-input prop="mapTableName" label="映射逻辑表格名称" type="text" :span="8" dataType="common"
                       v-model="pageData.formData.mapTableName"></zh-form-input>
        <zh-form-input prop="mapColumnType" label="映射字段类型" type="text" :span="8" dataType="common"
                        v-model="pageData.formData.mapColumnType"></zh-form-input>
        <zh-form-input prop="srcIdColumnName" label="源表格主键字段名称" type="text" :span="8" dataType="common"
                       v-model="pageData.formData.srcIdColumnName"></zh-form-input>
      </zh-form>
      <template #footer>
        <span class="dialog-footer">

          <el-button @click="pageData.addLogicMapDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="addLogicMap">
            保存
          </el-button>
        </span>
      </template>
    </el-dialog>
    <el-dialog title="修改逻辑表映射" v-model="pageData.editLogicMapDialogVisible">
      <zh-form :rules="pageData.formRules">
        <zh-form-input prop="mapId" label="表格映射主键"
                       v-model="pageData.editFormData.mapId"></zh-form-input>
        <zh-form-input prop="tableName" label="表格名称"
                       v-model="pageData.editFormData.tableName"></zh-form-input>
        <zh-form-input prop="dataSourcename" label="数据源名称"
                       v-model="pageData.editFormData.dataSourcename"></zh-form-input>
        <zh-form-input prop="datasourceId" label="所属数据源"
                       v-model="pageData.editFormData.datasourceId"></zh-form-input>
        <zh-form-input prop="srcTableId" label="源表格表id"
                       v-model="pageData.editFormData.srcTableId"></zh-form-input>
        <zh-form-input prop="srcTableName" label="源表格名称"
                       v-model="pageData.editFormData.srcTableName"></zh-form-input>
        <zh-form-input prop="srcColumnName" label="字段名称"
                       v-model="pageData.editFormData.srcColumnName"></zh-form-input>
        <zh-form-input prop="mapColumnName" label="映射字段名称"
                       v-model="pageData.editFormData.mapColumnName"></zh-form-input>
        <zh-form-input prop="srcColumnType" label="字段类型" type="text"
                        v-model="pageData.editFormData.srcColumnType"></zh-form-input>
        <zh-form-input prop="mapTableName" label="映射逻辑表格名称"
                       v-model="pageData.editFormData.mapTableName"></zh-form-input>
        <zh-form-input prop="mapColumnType" label="映射字段类型" type="text"
                        v-model="pageData.editFormData.mapColumnType"></zh-form-input>
        <zh-form-input prop="srcIdColumnName" label="源表格主键字段名称"
                       v-model="pageData.editFormData.srcIdColumnName"></zh-form-input>
      </zh-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="pageData.editLogicMapDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="editLogicMap">
            保存
          </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>
<script lang="ts" setup name="logicMap">
import {Delete, Edit, VideoPlay} from '@element-plus/icons-vue'
import {ElMessageBox} from "element-plus";
// ------------------接收传入参数(组件)-------------------

// ------------------钩子函数----------------------------
// 初始化
onMounted(() => {
  init();
})

// ------------------定义子组件--------------------------
const logicMapTable = ref();
const queryFormRef = ref();
// ------------------变量区------------------------------
const pageData = reactive({
  addLogicMapDialogVisible: false,
  editLogicMapDialogVisible: false,
  detailDialog: false,
  tableColumns: [
    {type: 'seq', width: 60, fixed: 'left', align: 'center', title: '序号'},
    {field: 'mapId', title: '表格映射主键', visible: false, align: 'center', width: 100},
    {field: 'tableName', title: '表格名称', visible: true, align: 'center', width: 100},
    {field: 'dataSourcename', title: '数据源名称', visible: true, align: 'center', width: 100},
    {field: 'datasourceId', title: '所属数据源', visible: true, align: 'center', width: 100},
    {field: 'srcTableId', title: '源表格表id', visible: true, align: 'center', width: 100},
    {field: 'srcTableName', title: '源表格名称', visible: true, align: 'center', width: 100},
    {field: 'srcColumnName', title: '字段名称', visible: true, align: 'center', width: 100},
    {field: 'mapColumnName', title: '映射字段名称', visible: true, align: 'center', width: 100},
    {field: 'srcColumnType', title: '字段类型', visible: true, align: 'center', width: 100},
    {field: 'mapTableName', title: '映射逻辑表格名称', visible: true, align: 'center', width: 100},
    {field: 'mapColumnType', title: '映射字段类型', visible: true, align: 'center', width: 100},
    {field: 'srcIdColumnName', title: '源表格主键字段名称', visible: true, align: 'center', width: 100},
    {field: 'op', title: '操作', visible: true, align: 'center', width: 140, fixed: 'right', slots: {default: 'op'}},
  ],
  queryParams: {
    mapId: '',
    tableName: '',
    dataSourcename: '',
    datasourceId: '',
    srcTableId: '',
    srcTableName: '',
    srcColumnName: '',
    mapColumnName: '',
    srcColumnType: '',
    mapTableName: '',
    mapColumnType: '',
    srcIdColumnName: ''
  },
  formData: {
    mapId: '',
    tableName: '',
    dataSourcename: '',
    datasourceId: '',
    srcTableId: '',
    srcTableName: '',
    srcColumnName: '',
    mapColumnName: '',
    srcColumnType: '',
    mapTableName: '',
    mapColumnType: '',
    srcIdColumnName: ''
  },
  editFormData: {
    mapId: '',
    tableName: '',
    dataSourcename: '',
    datasourceId: '',
    srcTableId: '',
    srcTableName: '',
    srcColumnName: '',
    mapColumnName: '',
    srcColumnType: '',
    mapTableName: '',
    mapColumnType: '',
    srcIdColumnName: ''
  },
  formRules: {},
  dictFields: '',
  dictData: [],
});
// ------------------方法区------------------------------
const init = () => {
  getDict();
  queryLogicMap();
}
// 查询数据字典信息
const getDict = () => {
  let params = {
    dictFields: pageData.dictFields,
  };
  $api.common.getBatchDict.post(params).then((res: any) => {
    pageData.dictData = res.result;
  });
};
const queryLogicMap = () => {
  logicMapTable.value.query(pageData.queryParams);
}
const showAddDialog = () => {
  pageData.addLogicMapDialogVisible = true;
  pageData.formData = {
    mapId: '',
    tableName: '',
    dataSourcename: '',
    datasourceId: '',
    srcTableId: '',
    srcTableName: '',
    srcColumnName: '',
    mapColumnName: '',
    srcColumnType: '',
    mapTableName: '',
    mapColumnType: '',
    srcIdColumnName: ''
  }
}
const showEditDialog = (scope) => {
  pageData.editLogicMapDialogVisible = true;
  pageData.editFormData = {...scope.row}
}
const addLogicMap = () => {
  $api.common.send.post('logicMap0004', pageData.formData).then(res => {
    if (res.code == 0) {
      ElMessageBox.alert('添加逻辑表映射成功');
      pageData.addLogicMapDialogVisible = false;
      queryLogicMap();
    }
  })
}
const editLogicMap = () => {
  $api.common.send.post('logicMap0005', pageData.editFormData).then(res => {
    if (res.code == 0) {
      ElMessageBox.alert('修改逻辑表映射成功');
      pageData.editLogicMapDialogVisible = false;
      queryLogicMap();
    }
  })
}
const delLogicMap = ({mapId}) => {
  ElMessageBox.confirm('确定删除该逻辑表映射?').then(() => {
    $api.common.send.post('logicMap0006', {mapId: mapId}).then(res => {
      if (res.code == 0) {
        ElMessageBox.alert('删除逻辑表映射成功');
        queryLogicMap();
      }
    })
  })
}

const updateLogicTableData = (row) => {
  $api.common.send.post('logicMap0007', {...row}).then(res => {
    if (res.code == 0) {
      ElMessageBox.alert('更新逻辑表数据成功');
    }
  })
}

</script>
<style lang="scss" scoped></style>
